﻿using PSI.Helper;
using PSI.Models.TMS;
using PSI.Services;
using PSI.Services.TMS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PSI.Controllers {
    public class AddressController : PSIController {
        public ActionResult Index() {
            var ps = new PermissionService();

            var cc = Helper.SimpleCrypt.Decode("LKL5dql2j8X09UjKTW2SZA==");


            if (!ps.HasPermission(FIdConst.ADDRESS)) {
                return Redirect("/");
            }

            ViewBag.UserName = UserService.GetLoginUserName();

            return View();
        }

        [HttpPost]
        public ActionResult GetListData() {
            var data = new AddressService().GetListData();

            return Json(data);
        }

        [HttpPost]
        public ActionResult GetimportListDataStore(string date)
        {
            
            var data = new InConsignationService().GetInConsignationDetailListData("").Where(a => a.isweixin == IsWXStatus.release||a.isweixin==IsWXStatus.complete);
            if (date.Contains("今天"))
            {
                data = data.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd"));
            } else if (date.Contains("明天")) {
                data = data.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"));
            } else if (date.Contains("昨天"))
            {
                data = data.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"));
            }
            var num = 1;
            var datas = (from item in data select new { id = num++, name = item.Address }).DistinctBy(a => a.name).ToList();
            var cc = new { dat = datas };
            return Json(cc);  
        }

        [HttpPost]
        public ActionResult GetexportListDataStore(string date)
        {

            var data = new OutConsignationService().GetOutConsignationDetailListData("").Where(a => a.isweixin == IsWXStatus.release || a.isweixin == IsWXStatus.complete);
            if (date.Contains("今天"))
            {
                data = data.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd"));
            } else if (date.Contains("明天"))
            {
                data = data.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"));
            }
            else if(date.Contains("昨天"))
            {
                data = data.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"));
            }
            var num = 1;
            var datas = (from item in data select new { id = num++, name = item.Address }).DistinctBy(a=>a.name).ToList();
            var cc = new { dat = datas };
            return Json(cc);
        }

        [HttpPost]
        public ActionResult Edit(Address address) {
            var result = new AddressService().Edit(address);

            return Json(result);
        }

        [HttpPost]
        public ActionResult Delete(string id) {
            var result = new AddressService().Delete(id);

            return Json(result);
        }

        [HttpPost]
        public ActionResult QueryData(string queryKey) {
            var data = new AddressService().QueryData(queryKey);

            return Json(data);
        }
    }
}
